package com.project.erp.dao.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.project.erp.common.dao.HibernateDao;
import com.project.erp.common.utils.Page;
import com.project.erp.model.system.Posts;

@Repository
public class PostsDao extends HibernateDao<Posts, Long> {
	/**
	 * 根据id列表删除
	 * @posts idsList
	 */
	public void delPostsBypostsIds(final List<Long> idsList){
		this.deleteByIdsList(idsList);
	}
	
	/**
	 * 根据系统参数标识查找系统参数记录
	 * @param postNum
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Posts getPostsByPostNum(String postNum){
		Criteria c = this.getSession().createCriteria(Posts.class);
		c.add(Restrictions.eq("postNum", postNum));

		List<Posts> posts = c.list();
		Posts post = null;
		
		if((posts != null) && (posts.size() > 0)){
			post = posts.get(0);
		}
		return post;
	}
	
	/**
	 * 根据条件查询
	 * @posts page
	 * @posts posts
	 * @return
	 */
	public Page<Posts> getPostsPageBySearch(Page<Posts> page,Posts posts){
		List<Criterion> list = new ArrayList<Criterion>();
		if(StringUtils.isNotEmpty(posts.getPostNum())){
			Criterion postNum = Restrictions.ilike("postNum",posts.getPostNum(),MatchMode.ANYWHERE);
			list.add(postNum);
		}
		if(StringUtils.isNotEmpty(posts.getPostName())){
			Criterion postName = Restrictions.ilike("postName",posts.getPostName(),MatchMode.ANYWHERE);
			list.add(postName);
		}
		
		page = this.findPage(page,list.toArray(new Criterion[list.size()]));
		
		return page;
	}
}
